
// A1_compute_market_shares
//==============================================================================

// Description: This file computes average market shares for each exporter that are
// then used in Appendix Figure A1

clear
clear matrix
set more off

cd "D:\data_replication"

// Import and format trade and income data
//------------------------------------------------------------------------------

use data\trade\EU_imports_cn8_plus_summary_USD.dta, clear

keep if year==2007


// Construct shares
//------------------------------------------------------------------------------

sort year cn8plus declarant
by year cn8plus declarant: egen quantity_total = sum(quantity_year_ton_cn8plus)
gen market_share = quantity_year_ton_cn8plus/quantity_total
gen market_share_log = log(market_share)
by year cn8plus declarant: egen imports_total = sum(imports_year_USD_cn8plus)
gen expenditure_share = imports_year_USD_cn8plus/imports_total
gen expenditure_share_log = log(expenditure_share)
gen price_log = log(price_ton_USD_cn8plus)

xtset cn8plus
xtreg market_share price_ton_USD_cn8plus, fe vce(cluster cn8plus)
xtreg market_share price_log, fe vce(cluster cn8plus)


// Create country dummies
//------------------------------------------------------------------------------

forval i=1/999 {
display `i'
gen s`i' = 0
replace s`i' = 1 if partner == `i'
egen sum_s = sum(s`i')
if sum_s[1]==0 {
drop s`i'
}
drop sum_s
}


merge m:1 partner year using data\gdp_per_capita\GDP_per_capita_defl.dta
keep if _merge == 3
drop _merge

gen gdp_capita_partner_log = log(gdp_per_capita_defl)


// Run regression and save output
//------------------------------------------------------------------------------

set more off
xtset cn8plus
xtreg market_share_log s3 - s979 , fe vce(cluster cn8plus)
estout using statistics_and_graphs\market_shares\output_files\market_shares_exporter.txt, replace

